package cn.net.autocode.platform.workflow;

import cn.net.autocode.platform.factory.SpringBeanFactory;
import cn.net.autocode.platform.workflow.service.WorkflowServ;
import cn.net.autocode.platform.workflow.vo.Workflow;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

public class WorkflowFactory {

    private static WorkflowServ workflowServ;

    public static JSONObject create(String wfCode,String busiId,String wfRecordTitle){
        if (workflowServ==null){
            workflowServ = SpringBeanFactory.getBeanByType(WorkflowServ.class);
        }

        JSONObject wfRecord =  workflowServ.create(wfCode,busiId,wfRecordTitle);

        return wfRecord;
    }

    public static JSONArray submit(String wfCode,String stepCode,String actionCode,String busiId){
       return WorkflowFactory.submit(wfCode,stepCode,actionCode,busiId,null);
    }

    public static JSONArray submit(String wfCode,String stepCode,String actionCode,String busiId,String comment){
        if (workflowServ==null){
            workflowServ = SpringBeanFactory.getBeanByType(WorkflowServ.class);
        }

        JSONObject param = new JSONObject();
        param.put("wfCode",wfCode);
        param.put("stepCode",stepCode);
        param.put("actionCode",actionCode);
        param.put("busiId",busiId);
        param.put("comment",comment);
        Workflow workflow = new Workflow();
        //信息校验
        String validateMsg = workflowServ.validate(param,workflow);
        if(!"success".equals(validateMsg)){
            throw new RuntimeException(validateMsg);
        }

        //获取校验信息
        workflowServ.before(workflow);
        workflowServ.action(workflow,"before");
        JSONArray nextSteps = workflowServ.after(workflow);
        workflowServ.action(workflow,"after");
        return nextSteps;
    }

    public static String delete(String wfCode,String busiId){
        if (workflowServ==null){
            workflowServ = SpringBeanFactory.getBeanByType(WorkflowServ.class);
        }

        String msg = workflowServ.delete(wfCode,busiId);
        if(!"success".equals(msg)){
            throw new RuntimeException(msg);
        }
        return msg;
    }

    public static String cancel(String wfCode,String busiId,String comment){
        if (workflowServ==null){
            workflowServ = SpringBeanFactory.getBeanByType(WorkflowServ.class);
        }

        JSONObject param = new JSONObject();
        param.put("wfCode",wfCode);
        param.put("busiId",busiId);
        param.put("comment",comment);
        Workflow workflow = new Workflow();
        //信息校验
        String validateMsg = workflowServ.cancelValidate(param,workflow);
        if(!"success".equals(validateMsg)){
            throw new RuntimeException(validateMsg);
        }

        //流程终止
        workflowServ.cancelDeal(workflow,"before");
        workflowServ.cancel(workflow);
        workflowServ.cancelDeal(workflow,"after");
        return "success";
    }


    public static String cancel(String wfCode, JSONArray busiIds, String comment){
        if (workflowServ==null){
            workflowServ = SpringBeanFactory.getBeanByType(WorkflowServ.class);
        }

        JSONObject param = new JSONObject();
        param.put("wfCode",wfCode);
        param.put("busiId",busiIds);
        param.put("comment",comment);
        Workflow workflow = new Workflow();
        //信息校验
        String validateMsg = workflowServ.cancelValidate(param,workflow);
        if(!"success".equals(validateMsg)){
            throw new RuntimeException(validateMsg);
        }

        //流程终止
        workflowServ.cancelDeal(workflow,"before");
        workflowServ.cancel(workflow);
        workflowServ.cancelDeal(workflow,"after");
        return "success";
    }
}
